<?php
namespace app\index\controller;

use app\index\controller\Base;
use app\common\model\Apply as ApplyModel;
use app\common\model\user as UserModel;
use app\common\model\SmsLog as SmsLogModel;

class Personal extends Base
{
    public function initialize()
    {
        parent::initialize();
        if(empty($this->current_user)){
            if($this->request->isAjax()){
                $this->error('请先登录', url('index/index'));
            }else{
                $this->redirect('index/index');
            }
        }

        $this->Model = new UserModel();
    }

    public function index()
    {
        $user_detail = $this->Model->currentDetail();
        $this->assign('user_detail', $user_detail);

        $gender_list = ['male' => '男', 'female' => '女'];
        $this->assign('gender_list', $gender_list);

        $ApplyModel = new ApplyModel();
        $default_param = ['user_id' => login_user_id()];
        $res = $ApplyModel->userPaginate($default_param);
        $this->assign('res', $res);

        // 设置页面SEO信息
        $this->setPageSeo([
            'title' => '个人中心',
        ]);

        return $this->fetch();
    }

    // 申报项目
    public function update(){
        if(!$this->request->isPost()){
            if($this->request->isAjax()){
                $this->error('访问的页面不存在', url('index'));
            }else{
                $this->redirect('index');
            }
        }

        if($this->request->isPost()){
            $fields = [
                ['name' => 'gender', 'type' => 'string', 'default' => ''],
                ['name' => 'birthday', 'type' => 'birthday', 'is_null' => true],
                ['name' => 'email', 'type' => 'string', 'default' => ''],
                ['name' => 'pro_title', 'type' => 'string', 'default' => ''],
                ['name' => 'college_name', 'type' => 'string', 'default' => ''],
            ];
            $default = [
                'id' => login_user_id(),
                'scene_name' => 'update_detail',
            ];
            $data = $this->parsePostParam($fields, $default);
            $res = $this->Model->updateItem($data);
            if($res["status"]){
                $this->success($res["msg"], url('index'));
            }else{
                $this->error($res["msg"], url('index'));
            }
        }
    }

    public function modify_mobile_step1(){
        if(!$this->request->isPost()){
            if($this->request->isAjax()){
                $this->error('访问的页面不存在', url('index/index'));
            }else{
                $this->redirect('index/index');
            }
        }
        $code = $this->request->post('code');
        $SmsLogModel = new SmsLogModel();
        $valid_res = $SmsLogModel->validSendCode($this->current_user['mobile'], $code);
        if($valid_res['status']){
            $this->success($valid_res['msg']);
        }else{
            $this->error($valid_res['msg']);
        }
    }

    public function modify_mobile_step2(){
        if(!$this->request->isPost()){
            if($this->request->isAjax()){
                $this->error('访问的页面不存在', url('index/index'));
            }else{
                $this->redirect('index/index');
            }
        }
        $fields = [
            ['name' => 'mobile', 'type' => 'string', 'default' => ''],
            ['name' => 'code', 'type' => 'string', 'default' => ''],
        ];
        $default = [
            'id' => $this->current_user['id'],
        ];
        $data = $this->parsePostParam($fields, $default);
        $SmsLogModel = new SmsLogModel();
        $valid_res = $SmsLogModel->validSendCode($data['mobile'], $data['code']);
        if(!$valid_res['status']){
            $this->error($valid_res['msg']);
        }

        $res = $this->Model->updateItem($data);
        if($res["status"]){
            $this->success($res["msg"], url('index'));
        }else{
            $this->error($res["msg"], url('index'));
        }
    }

    public function modify_password(){
        if(!$this->request->isPost()){
            if($this->request->isAjax()){
                $this->error('访问的页面不存在', url('index/index'));
            }else{
                $this->redirect('index/index');
            }
        }

        $fields = [
            ['name' => 'password', 'type' => 'string', 'default' => ''],
            ['name' => 'code', 'type' => 'string', 'default' => ''],
        ];
        $default = [
            'id' => $this->current_user['id'],
        ];
        $data = $this->parsePostParam($fields, $default);
        $SmsLogModel = new SmsLogModel();
        $valid_res = $SmsLogModel->validSendCode($this->current_user['mobile'], $data['code']);
        if(!$valid_res['status']){
            $this->error($valid_res['msg']);
        }

        $data['confirm_password'] = $data['password'];
        $res = $this->Model->updateItem($data);
        if($res["status"]){
            $this->success($res["msg"], url('index'));
        }else{
            $this->error($res["msg"], url('index'));
        }
    }
}
